<?
	include("../includes/main_inc.php");
	
	
	if($_POST['Delete']=="Delete"){
		if(is_array($_POST['DFiles'])){
			$m= new DeleteFromDatabase();
			$m->AddIDArray($_POST['DFiles']);
			$m->AddTable("affiliate");
			$Errors=$m->DoDelete();
			
			if($Errors==""){
				$Message="Members Deleted";
			}else{
				$Message=$Errors;
			};
		}else{
			$Message="No Members Selected To Delete";
		};
	};
	if($_POST['Approve']=="Approve"){
		$a=new BulkDBChange();
		$a->AddTable("affiliate");
		$a->AddIDArray($_POST['DFiles']);
		$a->WhatToChange("status","Approved");
		$a->DoChange();
	}
	if($_POST['Reject']=="Reject"){
		$a=new BulkDBChange();
		$a->AddTable("affiliate");
		$a->AddIDArray($_POST['DFiles']);
		$a->WhatToChange("status","Rejected");
		$a->DoChange();
	}
	
	if($_GET['SText']) $SText=$_GET['SText'];
	elseif ($_POST['SText']) $SText=$_POST['SText'];
	if($_GET['SType']) $SType=$_GET['SType'];
	elseif ($_POST['SType']) $SType=$_POST['SType'];
	if($_GET['OType']) $OType=$_GET['OType'];
	elseif ($_POST['OType']) $OType=$_POST['OType'];
	else $OType="contactName";
	if($_GET['Page']) $Page=$_GET['Page'];
	elseif($_POST['Page']) $Page=$_POST['Page'];
	else $Page=1;
	
	$RecordsPerPage=10;
	
	if(!empty($SText)){
		$SearchSQL="WHERE $SType LIKE '%$SText%'";
	};
	
	
	$SQL1="SELECT COUNT(*) FROM affiliate $SearchSQL";
	$rset=$r->rawQuery($SQL1);
	$rdata=mysql_fetch_array($rset);
	$rcount=$rdata[0];
	$MaxPages=ceil($rcount/$RecordsPerPage);
	if($Page>$MaxPages) $Page=$MaxPages;
	$StartRecord=($Page-1)*$RecordsPerPage;
	if($StartRecord<0) $StartRecord=0;
	$SQL2="SELECT id,contactName,username,email,status,Account FROM affiliate $SearchSQL  ORDER BY $OType LIMIT $StartRecord,$RecordsPerPage";
	$rset=$r->rawQuery($SQL2);
	//print $SQL2;
	$NPPage="SType=$SType&OType=$OType&SText=".urlencode($SText);
	$RecTo=($StartRecord+$RecordsPerPage);
	if($RecTo>$rcount) $RecTo=$rcount;
	
?><html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<title>Affiliate Bubble Administration</title>

<link href="<?=$rootdir?>css/admin.css" rel="stylesheet" type="text/css">
<script src="<?=$rootdir?>jscript/menuArray.js" language="javascript"></script>
<script src="<?=$rootdir?>jscript/clock.js" language="javascript"></script>
<style type="text/css">
<!--
.style1 {font-weight: bold}
-->
</style>
</head>

<body onLoad="funClock();">
<?php include("../includes/header.php"); ?>
<table width="100%" border="0" cellspacing="25" cellpadding="0">
<tr>
<td valign="top" width="240"><div id="menu">
  <?php include("../menu/affiliatesmenu.php"); ?>
</div></td>
<td align="center" valign="top">

<div id="title">
Member Search
</div>

<div id="search"> 
Enter search criteria.<br><br>
<form name="form1" method="post" action="search.php">
    <table border="0" cellspacing="0" cellpadding="0">
      <tr>
        <td width="60">Keywords</td>
        <td><input name="SText" type="text" class="formFields" id="SText" value="<?=$SText;?>" size="30"></td>
        <td width="66" align="center">Search By </td>
        <td><select name="SType" class="formFields" id="SType">
            <option value="contactName" <?=($SType=="contactName" ? "selected" :"");?>>Contact Name</option>
            <option value="username" <?=($SType=="username" ? "selected" :"");?>>User Name</option>
            <option value="email" <?=($SType=="email" ? "selected" :"");?>>Email</option>
			<option value="status" <?=($SType=="status" ? "selected" :"");?>>Status</option>
          </select>        </td>
        <td width="50" align="center">Sort By </td>
        <td><select name="OType" class="formFields" id="OType">
          <option value="contactName" <?=($OType=="contactName" ? "selected" :"");?>>Contact Name</option>
            <option value="username" <?=($OType=="username" ? "selected" :"");?>>User Name</option>
            <option value="email" <?=($OType=="email" ? "selected" :"");?>>Email</option>
			<option value="status" <?=($OType=="status" ? "selected" :"");?>>Status</option>
			<option value="Account" <?=($OType=="Account" ? "selected" :"");?>>Account</option>
        </select></td>
        <td width="160" align="center"><input name="Search" type="submit" class="formButtons" value="Search"></td>
      </tr>
    </table>
    
    <p align="right">&nbsp;</p>

  <div id="content">
    <table width="100%" border="0" cellpadding="5" cellspacing="1" bgcolor="#E9EEF5">
      <tr id="formHeader">
        <td><strong>Affiliates Name </strong></td>
        <td align="center"><strong>Username</strong></td>
        <td align="center"><strong>Email</strong></td>
        <td width="34" align="center"><strong>Options</strong></td>
        <td width="35" align="center"><strong>Account</strong></td>
        <td width="34" align="center"><strong>Status</strong></td>
        <td width="35" align="center"><strong>Select</strong></td>
      </tr>
      <?php
	$Count=0;
	if($rcount>0){
		while ($myrow = mysql_fetch_row($rset)) {
?>
      <tr bgcolor="<?=(($Count%2)==0 ? "#FFFFFF" : "#EBEBEB"); ?>">
        <td><?=$myrow[1];?></td>
        <td align="center"><?=$myrow[2];?></td>
        <td align="center"><?=$myrow[3];?></td>
        <td align="center"><a href="modify.php?id=<?=$myrow[0];?>">Modify</a></td>
        <td align="center">$<?=$myrow[5];?> <a href="pay.php?id=<?=$myrow[0]?>">Pay</a> </td>
        <td align="center"><?=$myrow[4];?></td>
        <td align="center"><input type="checkbox" name="DFiles[]" value="<?=$myrow[0];?>"></td>
      </tr>
      <?
			$Count++;
		}
	};
?>
      <tr>
        <td bgcolor="#FFFFFF">&nbsp;</td>
        <td align="center" bgcolor="#FFFFFF">&nbsp;</td>
        <td align="center" bgcolor="#FFFFFF"><input name="Approve" type="submit" id="Approve" value="Approve" class="formButtonSubmit"></td>
        <td colspan="2" align="center" bgcolor="#FFFFFF"><input name="Reject" type="submit" id="Reject" value="Reject" class="formButtonSubmit"></td>
        <td colspan="2" align="center" bgcolor="#FFFFFF"><input name="Delete" type="submit" id="Delete" value="Delete" class="formButtonSubmit"></td>
      </tr>
    </table>
  </div>
  <div id="results2">
    <p><?=($StartRecord+1);?> - <?=$RecTo;?> of <?=$rcount;?> Results</p>
    <p>
      <? if($Page>1){ ?>
      <a href="search.php?Page=<?=$Page-1;?>&<?=$NPPage?>" >&lt;&lt;Back</a>
      <? }; ?> 
      <?
		for($x=1;$x<=$MaxPages;$x++){
			?><a href="search.php?Page=<?=$x;?>&<?=$NPPage?>" > <?=$x?> </a><?
		};
	  ?>
      <?
						if($Page<$MaxPages){
					?>
      <a href="search.php?Page=<?=$Page+1;?>&<?=$NPPage?>" >Next &gt;&gt; </a>
      <?
						};
					?>
</p>
  </div>
</form>
</div>

<div id="results">
  <p><a href="csv.php">EXPORT TO .CSV</a> </p>
</div>

</td>
</tr>
</table>
</body>
</html>
